package gr.unipi.interfaces;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import gr.unipi.cinema.Show;
import gr.unipi.cinema.ShowData;

import javax.ws.rs.*;
import javax.ws.rs.core.*;

@Path("/show")
public class ShowResource {
	
	// The @Context annotation allows us to have certain contextual objects
    // injected into this class.
    // UriInfo object allows us to get URI information (no kidding).
    @Context
    UriInfo uriInfo;
 
    // Another "injected" object. This allows us to use the information that's
    // part of any incoming request.
    // We could, for example, get header information, or the requestor's address.
    @Context
    Request request;
    
    @GET
    @Path("list")
    @Produces("application/json; charset=UTF-8")
    public ShowData[] getMovieList(@QueryParam("showid") String idStr,
    		@QueryParam("movieid") String movieid,
    		@QueryParam("theaterid") String theaterid,
    		@QueryParam("time") String time) {
    	int id = 0;
        if ((idStr != null) && (idStr.length() > 0)) {
        	try {
        		id = Integer.parseInt(idStr);
        	}
        	catch (Exception ex) {
        		return null;
        	}
        }
        
        int movie = 0;
        if ((movieid != null) && (movieid.length() > 0)) {
        	try {
        		movie = Integer.parseInt(movieid);
        	}
        	catch (Exception ex) {
        		return null;
        	}
        }
        
        int theater = 0;
        if ((theaterid != null) && (theaterid.length() > 0)) {
        	try {
        		theater = Integer.parseInt(theaterid);
        	}
        	catch (Exception ex) {
        		return null;
        	}
        }
        
        Calendar timeCal = null;
        if ((time != null) && (time.length() > 0)) {
        	timeCal = Calendar.getInstance();
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        	
        	try {
				timeCal.setTime(sdf.parse(time));
				timeCal.set(Calendar.SECOND, 0);
				timeCal.set(Calendar.MILLISECOND, 0);
			} catch (ParseException e) {
				return null;
			}
        }
        
        return Show.getShowData(id, movie, theater, timeCal);
    }
}
